package cn.micro.eye.model.add;

import cn.micro.eye.commons.exception.MicroBadRequestException;
import cn.micro.eye.commons.utils.EmailUtils;
import cn.micro.eye.commons.utils.IdCardUtils;
import cn.micro.eye.commons.utils.TelUtils;
import cn.micro.eye.commons.validate.NotInEnum;
import cn.micro.eye.type.RegisterTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * 账号注册
 *
 * @author lry
 */
@Data
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class AccountRegister implements Serializable {

    // ===== 微信注册

    private String code;

    // ===== 通用属性

    @NotBlank(message = "姓名不能为空")
    private String name;
    @NotBlank(message = "账号不能为空")
    private String openId;
    @NotBlank(message = "密码不能为空")
    private String password;
    @NotNull(message = "注册类型不能为空")
    @NotInEnum(message = "注册类型非法", target = RegisterTypeEnum.class)
    private Integer registerType;

    public void validate() {
        if (RegisterTypeEnum.EMAIL.getValue() == registerType) {
            if (!EmailUtils.validate(openId)) {
                throw new MicroBadRequestException("邮箱地址非法");
            }
        } else if (RegisterTypeEnum.PHONE.getValue() == registerType) {
            if (!TelUtils.validateMobile(openId)) {
                throw new MicroBadRequestException("手机号码非法");
            }
        } else if (RegisterTypeEnum.ID_CARD.getValue() == registerType) {
            if (!IdCardUtils.isValidate18IdCard(openId)) {
                throw new MicroBadRequestException("身份证号码非法");
            }
        } else if (RegisterTypeEnum.WX.getValue() == registerType) {
            if (StringUtils.isBlank(code)) {
                throw new MicroBadRequestException("CODE不能为空");
            }
        }
    }

}
